package daoImpl;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import utility.Utility;
import dao.OrderProductDAO;
import domain.OrderProduct;
import domain.Orders;
import domain.Product;

public class OrderProductDaoImpl implements OrderProductDAO {

	protected static Logger log = Logger.getLogger(OrderProductDaoImpl.class);

	@Resource(name = "sessionFactory")
	SessionFactory sessionFactory;

	@Override
	public void addOrederProduct(OrderProduct orderProduct) {
		log.debug("Adding an order product ...");
		Session session = Utility.getSession(sessionFactory);
		session.save(orderProduct);
		log.debug("Added an order product successfully.");
	}

	@Override
	public void addProduct(OrderProduct orderProduct, Product product) {
		log.debug("Adding more product into an exist order product ...");
		Session session = Utility.getSession(sessionFactory);
		orderProduct.getProducts().add(product);
		session.save(orderProduct);
		log.debug("Added new product into order product successfully.");
	}

	@Override
	public void removeProduct(OrderProduct orderProduct, Product product) {
		log.debug("Removing an product out to order product ...");
		Session session = Utility.getSession(sessionFactory);
		Set<Product> setProduct = orderProduct.getProducts();
		if (setProduct.contains(product)) {
			setProduct.remove(product);
			log.debug("Removed product successfully.");
		}
		orderProduct.getProducts().clear();
		orderProduct.setProducts(setProduct);
		session.save(orderProduct);
	}

	@Override
	public List<OrderProduct> getOrderProduct(Orders order) {
		log.debug("Getting order product from an order ...");
		return null;
	}

}
